/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "votewidget.h"

#include <QObject>
#include <QWidget>
#include <QList>

#include <QHBoxLayout>
#include <QPushButton>

#include <QSignalMapper>

#include <QMouseEvent>

#include <QDebug>

#include "../globals.h"

bool MouseMotionNotifier :: eventFilter(QObject *pObj, QEvent *pEvent)
{
    if ( pEvent->type() == QEvent::MouseMove || pEvent->type() == QEvent::HoverMove )
    {
        qDebug() << "Message sent";
        emit mouseIsHovering();
    }
    else
    {
        return QObject::eventFilter(pObj,pEvent);
    }

    return true;
}


VoteWidget::VoteWidget(QWidget *parent) :
    QWidget(parent),actualVote(-1)
{
    this->setMouseTracking(true);
    this->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);

    pMainGrid = new QHBoxLayout(this);

    pSignalMapperClicks = new QSignalMapper(this);
    pSignalMapperOvers = new QSignalMapper(this);

    for ( unsigned int i = 0 ; i < NB_NOTES ; i++ )
    {
        listPMMN.push_back(new MouseMotionNotifier(this));

        listPStars.push_back(new QPushButton(QIcon(EMPTY_STAR_PATH),"",this));
        listPStars[i]->setFlat(true);
        listPStars[i]->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
        listPStars[i]->installEventFilter(listPMMN[i]);

        connect(listPStars[i], SIGNAL(clicked()), pSignalMapperClicks, SLOT(map()));
        pSignalMapperClicks->setMapping(listPStars[i], i);

        connect(listPMMN[i], SIGNAL(mouseIsHovering()), pSignalMapperOvers, SLOT(map()));
        pSignalMapperOvers->setMapping(listPMMN[i], i);

        pMainGrid->addWidget(listPStars[i]);
    }

    connect(pSignalMapperClicks, SIGNAL(mapped(int )),this, SLOT(voteRequest(int )));
    connect(pSignalMapperOvers, SIGNAL(mapped(int )),this, SLOT(hoverButton(int)));

    qDebug() << "VoteWidget created";
}

VoteWidget :: ~VoteWidget(void)
{
    for ( int i = 0 ; i < listPStars.size() ; i++ )
    {
        listPStars[i]->removeEventFilter(listPMMN[i]);
        delete listPMMN[i];
        delete listPStars[i];
    }

    qDebug() << "VoteWidget deleted";
}

void VoteWidget :: mouseMoveEvent(QMouseEvent* pME)
{
    qDebug() << "Mouse event ...";

    pME->accept();

    // If we are here, it's that the mouse is no longer hoverring a button
    actualOver=-1;
    setStarsIcon();
}

void VoteWidget :: setStarsIcon()
{
    for ( int i = 0 ; i < listPStars.size() ; i++ )
    {
        if ( (i <= actualVote && actualOver == -1) || (i <= actualOver) )
        {
            listPStars[i]->setIcon(QIcon(RED_STAR_PATH));
        }
        else
        {
            listPStars[i]->setIcon(QIcon(EMPTY_STAR_PATH));
        }
    }
}

void VoteWidget :: voteRequest(const int note)
{
    qDebug() << "VoteWidget :: voteRequest (" << note << ")";

    this->setActualVote(note);

    emit voteSignal(note);
}

void VoteWidget :: hoverButton(const int note)
{
    qDebug() << "VoteWidget :: overButton (" << note << ")";

    actualOver = note;
    setStarsIcon();
}

void VoteWidget :: reset(void)
{
    qDebug() << "VoteWidget :: reset";

    actualVote = -1;

    foreach(QPushButton* pButton , listPStars)
    {
        pButton->setIcon(QIcon(EMPTY_STAR_PATH));
    }

    this->setStarsIcon();
}

void VoteWidget :: setActualVote(const int newVote)
{
    qDebug() << "VoteWidget :: setActualVote (" << newVote << ")";

    actualVote = newVote;

    setStarsIcon();
}
